<template>
    <div class="app-container">
        <div class="header">
            <el-button type="primary" size="mini" @click="showSaveNotify">添加</el-button>
            <el-input style="width: 300px; margin-left: 100px;" v-model="notifyListForm.mimo" size="mini"
                placeholder="输入关键字搜索" @change="notifySearch" @input="checkVal">
                <i slot="suffix" class="el-icon-search"></i>
            </el-input>
        </div>

        <!-- 添加通知弹出框 -->
        <el-dialog title="添加通知" :visible.sync="showSaveNotyfyForm" :close-on-click-modal="false"
            @close="closeSaveNotifyForm">
            <el-form ref="saveNotifyForm" :model="saveNotifyForm" label-width="100px" :rules="saveNotifyRules">
                <el-form-item label="通知标题" prop="notifyName">
                    <el-input v-model="saveNotifyForm.notifyName"></el-input>
                </el-form-item>
                <el-form-item label="通知内容" prop="notifyMessage">
                    <el-input v-model="saveNotifyForm.notifyMessage"></el-input>
                </el-form-item>

                <el-form-item>
                    <template slot-scope="scope">
                        <el-button type="primary" @click="saveNotifyData" :loading="loading">提交</el-button>
                        <el-button @click="closeSaveNotifyForm">取消</el-button>
                    </template>
                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 修改通知弹出框 -->
        <el-dialog title="修改通知" :visible.sync="showEditNotifyForm" :close-on-click-modal="false"
            @close="closeEditNotifyForm">
            <el-form ref="editNotifyForm" :model="editNotifyForm" label-width="100px" :rules="editNotifyRules">
                <el-form-item label="通知标题" prop="notifyName">
                    <el-input v-model="editNotifyForm.notifyName"></el-input>
                </el-form-item>
                <el-form-item label="通知内容" prop="notifyMessage">
                    <el-input v-model="editNotifyForm.notifyMessage"></el-input>
                </el-form-item>
                <el-form-item>
                    <template slot-scope="scope">
                        <el-button type="primary" @click="editNotifyData" :loading="loading">提交</el-button>
                        <el-button @click="closeEditNotifyForm">取消</el-button>
                    </template>
                </el-form-item>
            </el-form>
        </el-dialog>

        <div class="content">
            <el-table :data="notifyList" style="width: 100%;" stripe>
                <el-table-column label="通知信息">
                    <el-table-column label="通知标题" prop="notifyName">

                        <template slot-scope="scope">
                            <div>{{ scope.row.notifyName }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="通知内容" prop="notifyMessage">

                        <template slot-scope="scope">
                            <div>{{ scope.row.notifyMessage }}</div>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column label="操作">
                    <el-table-column label="修改">

                        <template slot-scope="scope">
                            <el-button type="primary" circle icon="el-icon-edit"
                                @click="showEditNotify(scope.row.notifyId)"></el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="删除">
                        <template slot-scope="scope">
                            <el-button type="danger" circle icon="el-icon-delete"
                                @click="delNotify(scope.row.notifyId)"></el-button>
                        </template>
                    </el-table-column>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="notifySizeChange" @current-change="notifyCurrentChange"
                :page-size="notifyListForm.size" :page-sizes="[10, 20, 30]" layout="total, sizes, prev, pager, next"
                :total="notifyListTotal" style="margin-top: 20px;">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import utils from '@/utils/checkLogin'
import { getNotifyList, saveNotify, getUpdateNotifyInfo, editNotify, deleteNotify } from '../../api/api'
import { Message } from 'element-ui'
export default {
    data() {
        return {
            loading: false,
            showSaveNotyfyForm: false,
            notifyList: [],
            notifyListTotal: 0,
            notifyListForm: {
                mimo: '',
                page: 1,
                size: 10
            },
            showSaveNotyfyForm: false,
            saveNotifyForm: {
                notifyName: '',
                notifyMessage: ''
            },
            saveNotifyRules: {
                notifyName: [
                    { required: true, message: '请输入通知标题', trigger: 'change' }
                ],
                notifyMessage: [
                    { required: true, message: '请输入通知内容', trigger: 'change' }
                ]
            },
            showEditNotifyForm: false,
            editNotifyForm: {
                notifyId: null,
                notifyName: '',
                notifyMessage: null
            },
            editNotifyRules: {
                notifyName: [
                    { required: true, message: '请输入通知标题', trigger: 'change' }
                ],
                notifyMessage: [
                    { required: true, message: '请输入通知内容', trigger: 'change' }
                ]
            },
        }
    },
    methods: {
        getNotifyListData() {
            getNotifyList(this.notifyListForm).then((res) => {
                if (res.code == 200) {
                    console.log('通知列表', res)
                    this.notifyList = res.data
                    this.notifyListTotal = res.total
                }
            })
        },
        notifySizeChange(e) {
            this.notifyListForm.size = e
            this.getNotifyListData()
        },
        notifyCurrentChange(e) {
            this.notifyListForm.page = e
            this.getNotifyListData()
        },
        // 搜索
        notifySearch() {
            this.getNotifyListData()
        },
        // 如果搜索框的值为空，刷新一下通知列表
        checkVal() {
            if (this.notifyListForm.mimo == '') {
                this.getNotifyListData()
            }
        },
        saveNotifyData() {
            this.$refs.saveNotifyForm.validate(valid => {
                if (!valid) {
                    Message({
                        message: '请检查表单',
                        type: 'warning',
                        duration: 2000,
                    })
                    return false
                } else {
                    console.log(JSON.stringify(this.saveNotifyForm))
                    saveNotify(this.saveNotifyForm).then((res) => {
                        if (res.code == 200) {
                            console.log('添加通知', res)
                            Message({
                                message: '添加成功',
                                type: 'success',
                                duration: 2000
                            })
                            this.loading = true
                            this.getNotifyListData()
                            setTimeout(() => {
                                this.showSaveNotyfyForm = false
                                this.loading = false
                                this.$nextTick(() => {
                                    this.$refs.saveNotifyForm.resetFields()
                                })
                            }, 1500)
                        } else {
                            Message({
                                message: '添加失败',
                                type: 'warning',
                                duration: 2000
                            })
                        }
                    })
                }
            })
        },
        showSaveNotify() {
            this.showSaveNotyfyForm = true
        },
        closeSaveNotifyForm() {
            this.showSaveNotyfyForm = false
            this.$nextTick(() => {
                this.$refs.saveNotifyForm.resetFields()
            })
        },
        closeEditNotifyForm() {
            this.showEditNotifyForm = false
        },
        showEditNotify(notifyId) {
            this.showEditNotifyForm = true
            this.editNotifyForm.notifyId = notifyId
            getUpdateNotifyInfo(notifyId).then((res) => {
                if (res.code == 200) {
                    console.log('修改通知（数据回显）', res)
                    this.editNotifyForm.notifyName = res.data.notifyName
                    this.editNotifyForm.notifyMessage = res.data.notifyMessage
                }
            })

        },

        editNotifyData() {
            console.log(JSON.stringify(this.editNotifyForm))
            this.$refs.editNotifyForm.validate(valid => {
                if (!valid) {
                    Message({
                        message: '请检查表单',
                        type: 'warning',
                        duration: 2000,
                    })
                    return false
                } else {
                    editNotify(this.editNotifyForm).then((res) => {
                        if (res.code == 200) {
                            Message({
                                message: '修改成功',
                                type: 'success',
                                duration: 3000
                            })
                            console.log('修改通知', res)
                            this.loading = true
                            setTimeout(() => {
                                this.showEditNotifyForm = false
                                this.loading = false
                                this.getNotifyListData()
                                this.$nextTick(() => {
                                    this.$refs.editNotifyForm.resetFields()
                                })
                            }, 2000)

                        } else {
                            Message(({
                                message: '修改失败',
                                type: 'success',
                                duration: 2000
                            }))
                        }
                    })
                }
            })

        },
        delNotify(notifyId) {
            this.$confirm('确定删除该通知吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteNotify(notifyId).then((res) => {
                    if (res.code == 200) {
                        console.log('删除成功', res);
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            duration: 3000
                        })
                        this.getNotifyListData()
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败',
                            duration: 3000
                        })
                    }
                })
            }).catch(() => {
            })
        },
    },
    created() {
        utils.checkLogin()
        this.getNotifyListData()
    }
}
</script>

<style lang="less" scoped>
.app-container {
    padding: 40px;

    .header {
        align-items: center;
        display: flex;
        margin-bottom: 20px;

        .el-icon-search {
            cursor: pointer;
            margin-right: 4px;
            margin-top: 8px;
        }

    }

    .blueBack {
        .el-select-dropdown__item {
            height: 120px;
        }
    }

    .blueSecoend {
        .el-select-dropdown__item {
            height: 36px;
        }
    }

    .redBack {
        .el-select-dropdown__item {
            height: 100%;
        }
    }

    .redSecoend {
        .el-select-dropdown__item {
            height: 36px;
        }
    }


    .content {
        text-align: center;

        .el-select-dropdown__item {
            height: auto;
        }
    }
}
</style>